package com.xd.pdf.controller;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.nio.file.*;
import java.util.*;

/**
 * @ClassName PdfExtractor
 * @Description TODO
 * @Author Dell
 * @Date 2025/6/27
 **/
public class PdfExtractor {
    public static void main(String[] args) throws Exception {
        String excelPath = "C:\\Users\\Dell\\Desktop\\excel_deal\\东风签字.xlsx"; // Excel 文件路径
        String pdfFolderPath = "C:\\Users\\Dell\\Desktop\\excel_deal\\823060"; // 包含7149个PDF的文件夹路径
        String outputFolderPath = "C:\\Users\\Dell\\Desktop\\excel_deal\\out-1433"; // 输出匹配的PDF文件夹
        String unmatchedLogPath = "C:\\Users\\Dell\\Desktop\\excel_deal\\unmatched_acc-1433.txt"; // 没找到的ACC记录

        // 你可以动态调整sheet、列、起始行
        int sheetIndex = 0;       // 第一个sheet
        int columnIndex = 9;      // J列，索引从0开始
        int startRowIndex = 1;    // 从第二行开始读，跳过表头

        // 创建输出文件夹
        Files.createDirectories(Paths.get(outputFolderPath));

        // 读取Excel
        List<String> accList = readColumnFromExcel(excelPath, sheetIndex, columnIndex, startRowIndex);

        List<String> unmatched = new ArrayList<>();


        for (String acc : accList) {
            String pdfFileName = acc + ".pdf";
            Path pdfPath = Paths.get(pdfFolderPath, pdfFileName);
            if (Files.exists(pdfPath)) {
                Files.copy(pdfPath, Paths.get(outputFolderPath, pdfFileName), StandardCopyOption.REPLACE_EXISTING);
            } else {
                unmatched.add(acc);
            }
        }

        // 写入未找到的 ACC 到日志文件
        Files.write(Paths.get(unmatchedLogPath), unmatched);

        System.out.println("处理完成！共匹配：" + (accList.size() - unmatched.size()) + " 个，未匹配：" + unmatched.size() + " 个。");
    }

    /**
     * 读取指定Excel文件中，指定sheet的某一列数据，从指定起始行开始
     * @param excelFilePath Excel文件路径
     * @param sheetIndex sheet索引，从0开始，通常第一个sheet是0
     * @param columnIndex 需要读取的列索引，从0开始，比如J列是9
     * @param startRowIndex 起始读取行索引，从0开始，通常数据从1开始（跳过表头）
     * @return 读取到的该列数据列表（字符串形式）
     * @throws IOException 读取异常
     */
    private static List<String> readColumnFromExcel(String excelFilePath, int sheetIndex, int columnIndex, int startRowIndex) throws IOException {
        List<String> result = new ArrayList<>();

        try (FileInputStream fis = new FileInputStream(excelFilePath);
             Workbook workbook = new XSSFWorkbook(fis)) {

            Sheet sheet = workbook.getSheetAt(sheetIndex);

            for (int i = startRowIndex; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                if (row != null) {
                    Cell cell = row.getCell(columnIndex);
                    if (cell != null) {
                        if (cell.getCellType() == CellType.STRING) {
                            result.add(cell.getStringCellValue().trim());
                        } else if (cell.getCellType() == CellType.NUMERIC) {
                            result.add(String.valueOf((long) cell.getNumericCellValue()));
                        }
                    }
                }
            }
        }
        return result;
    }

}
